package com.kuang.thread.lock;

import java.util.concurrent.locks.ReentrantLock;

//测试lock锁
public class TestLock {
    public static void main(String[] args) {
        TestLock2 testLock1 = new TestLock2();

        new Thread(testLock1,"黄牛 ").start();
        new Thread(testLock1,"张三 ").start();
        new Thread(testLock1,"Neil ").start();
    }
}
class TestLock2 implements Runnable{
    int ticketNum =  10;

    //定义Lock锁
    //ReentrantLock 是一个可重入锁
    private final ReentrantLock lock  = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            try{
                lock.lock();//加锁
                if(ticketNum > 0){
                    System.out.println(Thread.currentThread().getName() + ticketNum--);
                }else{
                    break;
                }
            }finally {
                //解锁
                lock.unlock();
            }
        }
    }
}


